package net.actions.sys;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Iterator;

import org.apache.log4j.Logger;

import net.base.BaseAction;
import net.base.BaseDao;
import net.base.MD5;

public class UserAction extends BaseAction {
    private BaseDao dao = null;

    public BaseDao getDao() {
        return dao;
    }

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    private Logger log = Logger.getLogger(this.getClass());

    /**
     * 添加用户
     */
    public void insertUser() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());

            Map user = dao.getMap("user.selectUserBY_ACC", map);
            if (user != null) {
                this.setResult("01", "该账号已被使用，请换用其他账号！");
            } else {
                MD5 md5 = new MD5();
                String user_pwd = md5.getMD5ofStr(String.valueOf(map.get("USER_PWD")));
                map.put("USER_PWD", user_pwd);
                dao.insert("user.insertUser", map);
                this.setResult("00", "添加成功！");
            }
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "添加失败！\n" + e.toString());
            throw new Exception();
        }
    }

    /**
     * 修改用户密码
     */
    public void insertUserPwd() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());
            MD5 md5 = new MD5();
            String user_pwd = md5.getMD5ofStr(String.valueOf(map.get("USER_PWD")));
            map.put("USER_PWD", user_pwd);
            dao.update("user.updateUserPWD", map);
            this.setResult("00", "修改成功！");
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "修改失败！\n" + e.toString());
            throw new Exception();
        }
    }

    /**
     * 用户分配角色保存
     */
    public void insertUserRole() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());
            String user_dm = String.valueOf(map.get("USER_DM"));
            String[] role_dm = String.valueOf(map.get("ROLE_DM")).split(",");
            // 先删除原来的角色
            dao.delete("user.deleteUserRole", user_dm);
            // 插入新分配得角色
            for (int n = 0; n < role_dm.length; n++) {
                Map parMap = new HashMap();
                parMap.put("USER_DM", user_dm);
                parMap.put("ROLE_DM", role_dm[n]);
                dao.insert("user.insertUserRole", parMap);
            }
            this.setResult("00", "添加成功！");
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "添加失败！\n" + e.toString());
            throw new Exception();
        }
    }

    public void deleteUser() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());
            String[] user_dm = String.valueOf(map.get("IDS")).split(",");

            // 循环删除用户
            for (int n = 0; n < user_dm.length; n++) {
                // 先删除用户角色的关系
                dao.delete("user.deleteUserRole", user_dm[n]);

                // 删除用户
                dao.delete("user.deleteUser", user_dm[n]);

            }
            this.setResult("00", "删除成功！");
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "删除失败！\n" + e.toString());
            throw new Exception();
        }
    }

    /**
     * 取登录用户操作模块树
     */
    public void getModuleTreeByUserDM() {
        try {
            StringBuffer xml = new StringBuffer();
            String module_dm_sj = this.getRequest().getParameter("MODULE_DM_SJ");
            Map userMap = (Map) this.getSession().get("SYS_USER");
            String user_dm = String.valueOf(userMap.get("USER_DM"));
            xml.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            xml.append("<tree id=\"0\">");
            xml.append(getTreeString(module_dm_sj, user_dm));
            xml.append("</tree>");
            this.setResult("00", xml.toString());
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "操作失败！\n" + e.toString());
        }
    }

    private String getTreeString(String module_dm_sj, String user_dm) throws Exception {
        StringBuffer xml = new StringBuffer();
        Map parMap = new HashMap();
        parMap.put("MODULE_DM_SJ", module_dm_sj);
        parMap.put("USER_DM", user_dm);
        List list = dao.getList("user.selectModuleTreeByUserDM", parMap);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Map m = (Map) it.next();
            String text = "";
            if (m.get("MODULE_URL") == null || "".equals(m.get("MODULE_URL")) || "null".equals(m.get("MODULE_URL"))) {
                text = (String) m.get("MODULE_NAME");
            } else {
                String s = ((String) m.get("MODULE_URL")).indexOf("?") > 0 ? "&" : "?";
                String url = m.get("MODULE_URL") + s + "MODULE_DM=" + m.get("MODULE_DM");
                text = "&lt;a href=javascript:openTabInRightFrame('" + url + "','" + m.get("MODULE_NAME") + "','" + m.get("MODULE_DM") + "');&gt;" + m.get("MODULE_NAME") + "&lt;/a&gt;";
            }
            xml.append("<item text=\"" + text).append("\"").
                    append(" id=\"").append(m.get("MODULE_DM"))
                    .append("\" im0=\"tombs.gif\" im1=\"folderOpen.gif\" im2=\"iconSafe.gif\">");
            xml.append(getTreeString(String.valueOf(m.get("MODULE_DM")), user_dm));
            xml.append("</item>");
        }
        return xml.toString();
    }

}
